<template>
<a-layout style="min-height: 100%;margin: 0 auto;">
    <a-layout-sider v-model:collapsed="collapsed" collapsible theme="light" style="">
        <div class="logoLayout" style="display: flex;justify-content: center;align-items: center;text-align: center">
            <div class="logo">
                <img src="@/components/logo.jpg" style="width: 50px;height: 40px;">
            </div>
        </div>
        <a-menu v-model:selectedKeys="selectedKeys" mode="inline" theme="light">
            <a-menu-item key="home">
                <router-link to="/home">
                    <pie-chart-outlined />
                    <span>时间线</span>
                </router-link>
            </a-menu-item>

            <a-sub-menu key="productsRelated">
                <template #title>
                    <span>
                        <team-outlined />
                        <span>农产品信息相关</span>
                    </span>
                </template>

                <a-menu-item key="productLists">
                    <router-link to="/products">
                        <desktop-outlined />
                        <span>农产品信息</span>
                    </router-link>
                </a-menu-item>

                <a-menu-item key="productTypes">
                    <router-link to="/type">
                        <desktop-outlined />
                        <span>农产品类型</span>
                    </router-link>
                </a-menu-item>

                <a-menu-item key="productHistory">
                    <router-link to="/history">
                        <desktop-outlined />
                        <span>溯源记录</span>
                    </router-link>
                </a-menu-item>
            </a-sub-menu>

            <a-menu-item key="check">
                <router-link to="/check">
                    <desktop-outlined />
                    <span>产品检测</span>
                </router-link>
            </a-menu-item>

            <a-menu-item key="status">
                <router-link to="/position">
                    <desktop-outlined />
                    <span>产品运输</span>
                </router-link>
            </a-menu-item>

            <a-menu-item key="users">
                <router-link to="/users">
                    <desktop-outlined />
                    <span>所有用户信息</span>
                </router-link>
            </a-menu-item>

        </a-menu>
    </a-layout-sider>
    <a-layout>
        <a-layout-header style="background: white; padding: 0">
            <div style="text-align: center;display: flex;direction: right;">
                <h1 style="font-size: 30px;width: 90%;">
                    农产品信息登记系统
                </h1>
                <div style="width: 50px;width: 10%;">
                    <a-popover :title="this.user.name" trigger="click" overlayStyle="width: 100px;">
                        <template #content>
                            <router-link to="/userInfo">
                                个人信息
                            </router-link>
                            <a-button type="link" danger @click="logOut">退出</a-button>
                        </template>
                        <a-avatar :size="50" style="color: #f56a00; background-color: #fde3cf;text-align: center;">
                            {{ user.name }}
                        </a-avatar>

                    </a-popover>
                </div>
            </div>

        </a-layout-header>
        <a-layout-content style="margin: 0 16px">
            <a-breadcrumb class="breadcrumb" style="height: 30px;margin: 10px;font-size: 18px;">
                <a-breadcrumb-item v-for="(item, index) in breadList" :key="item.name">
                    <router-link v-if="item.name != name && index != 1 && index!=0" :to="{ path: item.path === '' ? '/' : item.path }">{{ item.meta.title }}</router-link>
                    <span v-else>{{ item.meta.title }}</span>
                </a-breadcrumb-item>
            </a-breadcrumb>
            <div class="children" :style="{minHeight: '800px',padding: '24px', background: '#fff'}">
                <router-view v-if="isRouterAlive"></router-view>
            </div>
        </a-layout-content>
        <!-- <a-layout-footer style="text-align: center">
            This is footer
        </a-layout-footer> -->
    </a-layout>
</a-layout>
</template>

<script>
import {
    PieChartOutlined,
    DesktopOutlined,
    UserOutlined,
    TeamOutlined,
    FileOutlined
} from '@ant-design/icons-vue';
import {
    message
} from 'ant-design-vue';
import {
    defineComponent,
    ref
} from 'vue';

export default defineComponent({
    components: {
        PieChartOutlined,
        DesktopOutlined,
        UserOutlined,
        TeamOutlined,
        FileOutlined,
    },
    provide(){
        return{
            reload:this.reload
        }
    },
    setup() {
        return {
            collapsed: ref(false),
            selectedKeys: ref(['1']),
            name: ref(''),
            breadList: ref([]),
            user: ref({
                name: "username"
            }),
            isRouterAlive:ref(true)
        };
    },
    created() {
        this.getBreadcrumb()
        this.user.name = window.sessionStorage.getItem("user")==null?window.localStorage.getItem("user"):window.sessionStorage.getItem("user")
        if(this.user.name==null){
            message.error("请重新登录！");
            this.$router.push('/login')
        }else{
            this.$router.push('/home')
        }
        
    },
    methods: {
        getBreadcrumb() {
            this.breadList = []
            this.name = this.$route.name
            this.$route.matched.forEach(item => {
                this.breadList.push(item)
            })
        },
        logOut() {
            message.success("退出成功！");
            window.sessionStorage.clear();
            window.localStorage.clear();
            this.$router.push("/login")
        },
        reload(){
            this.isRouterAlive = false
            this.$nextTick(function(){
                this.isRouterAlive = true;
            })
        }
    },
    watch: {
        $route() {
            this.getBreadcrumb()
        }
    }

});
</script>

<style>
.logo {
    height: 40px;
    width: 50px;
    margin: 10px;
    background-color: cadetblue;
}

.site-layout .site-layout-background {
    background: #fff;
}

[data-theme='light'] .site-layout .site-layout-background {
    background: cadetblue;
}
</style>
